<?php
/*
 * Created on 2011-2-18
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
//$siteRoot=trim($_SERVER['SCRIPT_FILENAME'],strstr($_SERVER['SCRIPT_FILENAME'],'/web'));
$siteRoot = dirname(__FILE__);
 define( "SITE_ROOT",$siteRoot);
 define ("FILES_ROOT",SITE_ROOT . '/files');
 //增加可移植性
$scriptUri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$baseName = basename($_SERVER['PHP_SELF']);
$domain = rtrim(str_replace($baseName,'',$scriptUri),'/');

 define ('DOMAIN',$domain);
 error_reporting(0);
 require_once(SITE_ROOT."/function/YiLook.php");
 require_once(SITE_ROOT."/function/Generator.php");
 require_once(SITE_ROOT."/function/FileAction.php");
 
	$result=array();
	$result[0] = array('state' =>1,'url'=>'');
	$result[1] = array('state' =>0,'erorr'=>'地址不能为空');	
	$result[2] = array('state' =>-1,'erorr'=>'url类型不正确');
	$result[3] = array('state' => -2, 'erorr' =>"抓取内容出错");
		

		
	$chapterUrl=trim($_POST['chapter_url']);
	//简单错误处理
	if(empty($chapterUrl)) 
	{//$errorMessage="地址不能为空";
		$error  = $result[1];
		
	}		
	elseif(false === strpos($chapterUrl,'art_')) {
		$error = $result[2];
	}else {
	
		
	$type=isset($_POST['type'])?trim($_POST['type']):'txt';
	
	set_time_limit(0);
	try {
		$g=new Generator($chapterUrl);
		$file=$g->write(FILES_ROOT);
	} catch (Exception $e) {
		$error = $result[3];	
	}
	
	}
	
	if (isset($error)){
		echo json_encode($error);
		die();
	}else {
		
	//假设采集生成如下文件，本地测试用	
	//$file = FILES_ROOT . '/test2.txt'; 

	if($type =='zip'){	
		$destination = str_replace('txt','zip',$file);
		// =FILES_ROOT .'/facon_'.uniqid().'.zip';
	
		$downFile=new FileAction();
		$zip = $downFile->create_zip($file,$destination);
		$document = basename($zip);
		//假设完毕
	}else {
		$document = basename($file);
	}
	$resource = DOMAIN .'/files/'.$document;
	
	session_start();
	$uid = uniqid();
	$_SESSION['file'] = array($uid=>$document);
				
	//$success = array('state' =>1,'url'=>iconv ('gbk','utf-8',$resource));
	$success = array('state' =>1,'url'=>DOMAIN . '/down.php?id=' .$uid );
	echo json_encode($success);
	//	die();
		
	} 
	
	
	/*
		if ($file!==false)
	$downFile=new FileAction();
	$fileInfo=pathinfo($file);
		
	
	if ($type=='zip'){
		//生成zip文件 下载
		$displayName=$fileInfo['filename'].'.zip';
		$downFile->download_zip($file,$displayName);
	}else{
		//生成txt文件下载
		$displayName=$fileInfo['filename'].'.txt';
		$downFile->download_txt($file);
	}
*/
	
	

 
?>
